(function() {
    'use strict';
    angular.module('webApp')
        .directive("checkedpaper", function() {
            return {
                restrict: 'E',
                template: "	<select ng-model='papers' ng-change='setPaper()' ng-init='PaperAction()' ng-options='paper.name for paper in pageFormats'></select>",
                replace: true,
                scope: {
                    // models: '=',//纸张模板数组
                    pheight: '@',
                    pwidth: '@',
                    // toStautsEn: "@", // 将要迁移到哪个状态： P_PAY_PART , P_PAY_FINISH
                    callback: "&", //处理完毕回调函数
                },
                controller: ['$scope', 'dataService', '$timeout', 'helper', '$sce',
                    function($scope, dataService, $timeout, helper, $sce) {
                        	
                        $scope.PaperAction = function() {
                            $scope.pageFormats = [ // Size in pt of various paper formats
                                {
                                    name: 'a0',
                                    value: [2383.94, 3370.39]
                                }, {
                                    name: 'a1',
                                    value: [1683.78, 2383.94]
                                }, {
                                    name: 'a2',
                                    value: [1190.55, 1683.78]
                                }, {
                                    name: 'a3',
                                    value: [841.89, 1190.55]
                                }, {
                                    name: 'a4',
                                    value: [595.28, 841.89]
                                }, {
                                    name: 'a5',
                                    value: [419.53, 595.28]
                                }, {
                                    name: 'a6',
                                    value: [297.64, 419.53]
                                }, {
                                    name: 'a7',
                                    value: [209.76, 297.64]
                                }, {
                                    name: 'a8',
                                    value: [147.40, 209.76]
                                }, {
                                    name: 'a9',
                                    value: [104.88, 147.40]
                                }, {
                                    name: 'a10',
                                    value: [73.70, 104.88]
                                }, {
                                    name: 'b0',
                                    value: [2834.65, 4008.19]
                                }, {
                                    name: 'b1',
                                    value: [2004.09, 2834.65]
                                }, {
                                    name: 'b2',
                                    value: [1417.32, 2004.09]
                                }, {
                                    name: 'b3',
                                    value: [1000.63, 1417.32]
                                }, {
                                    name: 'b4',
                                    value: [708.66, 1000.63]
                                }, {
                                    name: 'b5',
                                    value: [498.90, 708.66]
                                }, {
                                    name: 'b6',
                                    value: [354.33, 498.90]
                                }, {
                                    name: 'b7',
                                    value: [249.45, 354.33]
                                }, {
                                    name: 'b8',
                                    value: [175.75, 249.45]
                                }, {
                                    name: 'b9',
                                    value: [124.72, 175.75]
                                }, {
                                    name: 'b10',
                                    value: [87.87, 124.72]
                                }, {
                                    name: 'c0',
                                    value: [2599.37, 3676.54]
                                }, {
                                    name: 'c1',
                                    value: [1836.85, 2599.37]
                                }, {
                                    name: 'c2',
                                    value: [1298.27, 1836.85]
                                }, {
                                    name: 'c3',
                                    value: [918.43, 1298.27]
                                }, {
                                    name: 'c4',
                                    value: [649.13, 918.43]
                                }, {
                                    name: 'c5',
                                    value: [459.21, 649.13]
                                }, {
                                    name: 'c6',
                                    value: [323.15, 459.21]
                                }, {
                                    name: 'c7',
                                    value: [229.61, 323.15]
                                }, {
                                    name: 'c8',
                                    value: [161.57, 229.61]
                                }, {
                                    name: 'c9',
                                    value: [113.39, 161.57]
                                }, {
                                    name: 'c10',
                                    value: [79.37, 113.39]
                                }, {
                                    name: 'dl',
                                    value: [311.81, 623.62]
                                }, {
                                    name: 'letter',
                                    value: [612, 792]
                                }, {
                                    name: 'government-letter',
                                    value: [576, 756]
                                }, {
                                    name: 'legal',
                                    value: [612, 1008]
                                }, {
                                    name: 'junior-legal',
                                    value: [576, 360]
                                }, {
                                    name: 'ledger',
                                    value: [1224, 792]
                                }, {
                                    name: 'tabloid',
                                    value: [792, 1224]
                                }, {
                                    name: 'credit-card',
                                    value: [153, 243]
                                }, {
                                    name: '快递模板',
                                    value: [654.13, 360.23]
                                }
                            ];
                            $timeout(function() {
                                console.log("$scope.pageFormats[4]",$scope.pageFormats);
                                $scope.papers = $scope.pageFormats[41];
                                var width = 0,
                                    height = 0;
                                if ($scope.pwidth == "undefined" && $scope.pwidth == "undefined") {
                                    console.log("111111111");
                                    $scope.setPaper();
                                } else {
                                    console.log("12312321");
                                    var notnull=true;
                                    _.map($scope.pageFormats, function(v,index) {

                                        width = v.value[0] * 96 / 72,
                                        height = v.value[1] * 96 / 72;
                                       console.log(width,v.value[0] , 96 / 72);
                                        if (width == $scope.pwidth && height == $scope.pheight) {
                                        	notnull=false;
                                            $scope.papers = v;

                                        }else if(width != $scope.pwidth && height != $scope.pheight&&index==$scope.pageFormats.length-1&&notnull){
                                        	  $scope.papers = null;
                                        }
                                    });
                                }
                            })

                            //		console.log($scope.paperc)
                            //
                        }
                        $scope.setPaper = function() {
                            $scope.callback({
                                'paper': $scope.papers
                            });
                        }
                    }
                ],
                link: function(scope, element, attrs, ctrl) {
                }
            };
        });
})();